package com.example.mianshiguan.service.impl;

import com.example.mianshiguan.api.InterviewerConfigService;
import com.example.mianshiguan.api.dto.InterviewerConfigDTO;
import com.example.interviewer.domain.DigitalInterviewers;
import com.example.interviewer.mapper.DigitalInterviewersMapper;
import com.example.candiates.domain.InterviewSchedule;
import com.example.candiates.mapper.CandidateInterviewScheduleMapper;
import com.example.candiates.domain.InterviewQuestion;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;

@Service
public class InterviewerConfigServiceImpl implements InterviewerConfigService {
    
    @Autowired
    private DigitalInterviewersMapper digitalInterviewersMapper;
    
    @Autowired
    private CandidateInterviewScheduleMapper candidateInterviewScheduleMapper;
    
    @Override
    public InterviewerConfigDTO getConfig(Integer candidateId) {
        // 获取候选人的最新面试安排
        InterviewSchedule schedule = candidateInterviewScheduleMapper.getLatestScheduleByCandidateId(candidateId);
        if (schedule == null) {
            throw new RuntimeException("未找到候选人的面试安排");
        }
        
        // 获取数字人面试官配置
        DigitalInterviewers interviewer = digitalInterviewersMapper.selectDigitalInterviewersById(schedule.getDigitalInterviewerId().longValue());
        if (interviewer == null) {
            throw new RuntimeException("未找到数字人面试官配置");
        }
        
        // 构建配置DTO
        InterviewerConfigDTO config = new InterviewerConfigDTO();
        config.setId(interviewer.getId().intValue());
        config.setInterviewerId(interviewer.getId().intValue());
        config.setPositionName(interviewer.getExpertise());
        config.setTechnicalRequirements(interviewer.getSkills());
        config.setCommunicationRequirements("良好的沟通表达能力，能够清晰表达技术方案");
        config.setProblemSolvingRequirements("具备问题分析和解决能力，能够独立处理技术难题");
        config.setPromptTemplate(interviewer.getPromptTemplate());
        config.setTemperature(interviewer.getTemperature());
        
        // 动态获取题目
        if (schedule.getQuestionSetIds() != null && !schedule.getQuestionSetIds().isEmpty()) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                java.util.List<Integer> setIds = objectMapper.readValue(schedule.getQuestionSetIds(), new TypeReference<java.util.List<Integer>>(){});
                java.util.List<InterviewQuestion> questions = candidateInterviewScheduleMapper.getQuestionsBySetIds(setIds);
                if (questions != null && !questions.isEmpty()) {
                    config.setQuestion(questions.get(0).getTitle());
                } else {
                    config.setQuestion("未找到题目");
                }
            } catch (Exception e) {
                config.setQuestion("题目解析失败");
            }
        }
        
        return config;
    }
} 